package com.company.Tree;


/**
 * @author VX5
 * @Title: MJC
 * @ProjectName DataStructure
 * @Description: TODO
 * @date ${DAT}19:46
 */
public class BinaryTreeDemo {
    public static void main(String[] args) {
        BinaryTree binaryTree = new BinaryTree();

        //创建节点
        Tree root = new Tree(1,"宋江");
        Tree node1 = new Tree(2,"吴用");
        Tree node2 = new Tree(3,"卢俊义");
        Tree node3 = new Tree(4,"林冲");

        root.setLeft(node1);
        root.setRight(node2);
        node2.setRight(node3);
        binaryTree.setRoot(root);
        binaryTree.preOrder();
        System.out.println("/////");
        binaryTree.postOrder();

        System.out.println("/////");
        binaryTree.infixOrder();

        System.out.println("前序查找");
        System.out.println(binaryTree.preOrderSeacher(4).toString());
    }

    static class BinaryTree{
        private Tree root;

        public void setRoot(Tree root) {
            this.root = root;
        }

        //前序遍历
        void preOrder(){
            if (this.root!=null){
                root.preOrder();
            }else {
                System.out.println("树为空");
            }
        }

        void postOrder(){
            if (this.root!=null){
                root.postOrder();
            }else {
                System.out.println("树为空");
            }
        }

        void infixOrder(){
            if (this.root!=null){
                root.infixOrder();
            }else {
                System.out.println("树为空");
            }
        }

        Tree preOrderSeacher(int no){
            if (root!=null) {
                return root.preOrderSearch(no);
            }else {
                return null;
            }
        }

        void delNode(int no){
            if (root!= null) {
                if (root.getNo() == no) {
                    root = null;
                } else {
                    root.delNode(no);
                }
            }
        }

    }

}
